import os from "node:os";
import { fileURLToPath } from "node:url";
import { execa } from "execa";

const TIMEOUT = 10_000;

const root = new URL("../", import.meta.url);
const cwd = os.platform() === "win32" ? fileURLToPath(root) : root; // execa bug: fileURLToPath required on Windows
const cmd = "./bin/cli.js";

describe("YAML features", () => {
  test(
    "merge",
    async () => {
      const result = await execa(cmd, ["./test/fixtures/yaml-merge.yaml"], {
        cwd,
      });
      expect(result.stdout).toBe(`/**
 * This file was auto-generated by openapi-typescript.
 * Do not make direct changes to the file.
 */

export interface paths {
    "/admin/ping": {
        parameters: {
            query?: never;
            header?: never;
            path?: never;
            cookie?: never;
        };
        /** Ping pongs */
        get: operations["AdminPing"];
        put?: never;
        post?: never;
        delete?: never;
        options?: never;
        head?: never;
        patch?: never;
        trace?: never;
    };
}
export type webhooks = Record<string, never>;
export interface components {
    schemas: {
        /** @description represents an error message response. */
        HTTPError: {
            title?: string;
            detail?: string;
            status?: number;
            error?: string;
        };
    };
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: never;
}
export type $defs = Record<string, never>;
export interface operations {
    AdminPing: {
        parameters: {
            query?: never;
            header?: never;
            path?: never;
            cookie?: never;
        };
        requestBody?: never;
        responses: {
            /** @description OK */
            200: {
                headers: {
                    [name: string]: unknown;
                };
                content: {
                    "text/plain": string;
                };
            };
            /** @description Error response */
            "4XX": {
                headers: {
                    [name: string]: unknown;
                };
                content: {
                    "application/json": components["schemas"]["HTTPError"];
                };
            };
        };
    };
}`);
    },
    TIMEOUT,
  );

  test(
    "not ignore path item components in paths",
    async () => {
      const result = await execa(cmd, ["./test/fixtures/path-item-components.yaml"], {
        cwd,
      });
      expect(result.stdout).toBe(`/**
 * This file was auto-generated by openapi-typescript.
 * Do not make direct changes to the file.
 */

export interface paths {
    "/users": components["pathItems"]["users"];
}
export type webhooks = Record<string, never>;
export interface components {
    schemas: never;
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: {
        users: {
            parameters: {
                query?: never;
                header?: never;
                path?: never;
                cookie?: never;
            };
            get?: never;
            put?: never;
            /** Create user */
            post: operations["createUser"];
            delete?: never;
            options?: never;
            head?: never;
            patch?: never;
            trace?: never;
        };
    };
}
export type $defs = Record<string, never>;
export interface operations {
    createUser: {
        parameters: {
            query?: never;
            header?: never;
            path?: never;
            cookie?: never;
        };
        /** @description The user to create */
        requestBody?: {
            content: {
                "application/json": {
                    name?: string;
                };
            };
        };
        responses: {
            /** @description The user was created successfully */
            200: {
                headers: {
                    [name: string]: unknown;
                };
                content: {
                    "application/json": {
                        id?: string;
                        name?: string;
                    };
                };
            };
            /** @description Bad request */
            400: {
                headers: {
                    [name: string]: unknown;
                };
                content?: never;
            };
        };
    };
}`);
    },
    TIMEOUT,
  );
});
